package com.ulife.category.module.action;

import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.turbine.dataresolver.FormGroup;
import com.ulife.category.dao.CategoryDao;
import com.ulife.category.module.screen.Catelist;
import com.ulife.category.module.vo.CategoryVo;

public class CateAction {

	private static final Logger log = LoggerFactory.getLogger(Catelist.class);

	@Autowired
	private CategoryDao categoryDao;

	public void doAddCate(@FormGroup("addcate") CategoryVo cateVo, Navigator nav) {
		try {
			cateVo.setcStatus(1);
			int res = categoryDao.insertCategory(cateVo);
			if (res <= 0) {
				nav.redirectTo("common").withTarget("error");
			}
		} catch (SQLException e) {
			log.error("insert cate error.", e);
		}
		if (cateVo.getcParentid() != null) {
			nav.redirectTo("cateLink").withTarget("catelist").withParameter("id", cateVo.getcParentid().toString());
		} else {
			nav.redirectTo("cateLink").withTarget("catelist");
		}
	}

	public void doUpdateCate(@FormGroup("updatecate") CategoryVo cateVo, Navigator nav) {
		try {
			if (cateVo.getcCode() == null) {
				cateVo.setcCode("");
			}
			if (cateVo.getcNotes() == null) {
				cateVo.setcNotes("");
			}
			int res = categoryDao.updateCategory(cateVo);
			if (res <= 0) {
				nav.redirectTo("common").withTarget("error");
			}
		} catch (SQLException e) {
			log.error("insert cate error.", e);
		}
		if (cateVo.getcParentid() != null) {
			nav.redirectTo("cateLink").withTarget("catelist").withParameter("id", cateVo.getcParentid().toString());
		} else {
			nav.redirectTo("cateLink").withTarget("catelist");
		}
	}

	public void doDelCate(@FormGroup("delcate") CategoryVo cateVo, Navigator nav) {
		try {
			cateVo.setcStatus(0);
			int res = categoryDao.updateCategory(cateVo);
			if (res <= 0) {
				nav.redirectTo("common").withTarget("error");
			}
		} catch (SQLException e) {
			log.error("doDelCate cate error.", e);
		}
		return;
	}

}
